package cn.cjp.m3d27;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class iCollections {
    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        //Collections集合工具类方法
        Collections.addAll(list,"a","b","c","d");
        System.out.println(list);

        //打乱排序
        Collections.shuffle(list);
        System.out.println(list);

        //排序
        Collections.sort(list);
        System.out.println(list);
        System.out.println("================");

        //若被排序的元素是自定义类，那类中就必须实现comparable<>接口，并重写comparato方法
        ArrayList<jperson> list1 = new ArrayList<>();
        list1.add(new jperson(1));
        list1.add(new jperson(4));
        list1.add(new jperson(3));
        Collections.sort(list1);
        System.out.println(list1);
        System.out.println("====================");

        //Comparator
        ArrayList<jperson> list2 = new ArrayList<>();
        list2.add(new jperson(1));
        list2.add(new jperson(4));
        list2.add(new jperson(3));
        Collections.sort(list2, new Comparator<jperson>() {
            @Override
            public int compare(jperson o1, jperson o2) {
                //年龄升序
                return o1.getAge()-o2.getAge();
            }
        });
        System.out.println(list2);
    }
}
